package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import types.Block;

public class ProgramBlocksWindow extends JPanel  {
	private static final long serialVersionUID = 1L;
	private static Block[] Blocks;
	private static int maxHeight = 0;
	private static int maxWitdh = 350;
	private static Point[] points;
	
	public ProgramBlocksWindow() { 

		// init maxHeight
		if(maxHeight == 0)
		{
			for( int i=0 ; i<Blocks.length ; i++)
			{
				maxHeight += Blocks[i].getLine()*3 + 10 ;
			}
			maxHeight += 100;
		}
		
	}
	
	public static JPanel getAllBlocksPanel(Block[] b)
	{   
		Blocks = b;
		points = new Point[Blocks.length];
		JPanel allBlocksPanel = new JPanel();
		allBlocksPanel.setLayout(null);
		Insets insets = allBlocksPanel.getInsets();
		
		//init blocksLocations
		int x = 50;
		int y = 10;
		int height = 0;
		
		for( int i=0 ; i<Blocks.length ; i++)
		{
			 points[i] = new Point( x, y );	// save block position
			 height = Blocks[i].getLine()*3 ;
			 y += height + 10;
			 
		}
		//
		
		// add blocks to blocksPanel
		JPanel blocksPanel = (JPanel) createFrame().getContentPane();
		blocksPanel.setPreferredSize( new Dimension(350, maxHeight) );
		Dimension size = blocksPanel.getPreferredSize();
		blocksPanel.setBounds(25 + insets.left, 5 + insets.top,size.width, size.height);
		allBlocksPanel.add( blocksPanel );
        	
		// add checkbox to all blocks
		for(int ind=0; ind<points.length; ind++)
		{
			if(Blocks[ind].getType()=="regular")	continue;
			
			final JCheckBox[] cb = new JCheckBox[points.length];
			cb[ind] = new JCheckBox();
		    cb[ind].setSelected(true);
		    size = cb[ind].getPreferredSize();
		    cb[ind].setBounds(points[ind].x, points[ind].y, size.width, size.height);
		    cb[ind].addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {	
					int index = -1;
		        	for(int j=0; j<cb.length; j++)
		        	{
		        		if(cb[j] == e.getSource())
		        		{
		        			index = j;
		        			if( Blocks[index].getChecked()==true )
		        				Blocks[index].setChecked(false);
		        			else
		        				Blocks[index].setChecked(true);	
		        			
		        		}
		        	}			
				}
			});
		      
		    allBlocksPanel.add( cb[ind] );		      
		}
		
		
        //JPanel allBlocksPanel = (JPanel) createFrame().getContentPane();
        allBlocksPanel.setPreferredSize(new Dimension( maxWitdh, maxHeight )); 
        allBlocksPanel.repaint();
        allBlocksPanel.revalidate();

        return allBlocksPanel;
	}
	
	public static JFrame createFrame()
	{
		JFrame f = new JFrame();
		f.add(new ProgramBlocksWindow(), null);
        
	    return f;
	}
	
	public void paint(Graphics g) {
	  int height = 0;
	  int width = 350;
	  int lines=1;
	  Font regFont = g.getFont();
	  
	  // blocks color
	  Color RegularColor = new Color (192, 192, 192);
	  Color IfColor = new Color (51, 153, 255);
	  Color IfElseColor = new Color (102, 204, 0);
	  Color ForColor = new Color (153, 153, 255);
	  Color WhileColor = new Color (255, 102, 178);
	  Color FuncColor = new Color (255, 178, 102);
	  //
  
	  int i;
	  for( i=0 ; i<Blocks.length ; i++)
	  {
		  if( Blocks[i].getType()=="regular" )		g.setColor(RegularColor);
		  if( Blocks[i].getType()=="if" )			g.setColor(IfColor);
		  if( Blocks[i].getType()=="if_else" )		g.setColor(IfElseColor);
		  if( Blocks[i].getType()=="for" )			g.setColor(ForColor);
		  if( Blocks[i].getType()=="while" )		g.setColor(WhileColor);
		  if( Blocks[i].getType()=="func" )			g.setColor(FuncColor);
		  
		  height = Blocks[i].getLine()*3 ;
		 
		  
		  g.fillRect( points[i].x, points[i].y, width, height ); 	// draw block
		  g.setColor(Color.WHITE);
		  g.setFont(g.getFont().deriveFont(12.0f));
		  g.drawString(Blocks[i].getType(), points[i].x+5, points[i].y+10);		// Type of block text
		  g.drawString("lines:"+lines+"-"+(lines+Blocks[i].getLine()-1), points[i].x+100, points[i].y+10);		// lines number text
		  
		  lines += Blocks[i].getLine();
		  
		  
		  g.setFont(regFont);		// restore original font
		  /*
		   * 	int x = 100;
	  			int y = 10;
			  g.fillRect(x, y, 10*Blocks[i].getLine(), 15); 	// draw block
			  g.drawString(Blocks[i].getType(), x-60, y+10);	// Type of block text
			  g.setColor(Color.WHITE);
			  g.setFont(g.getFont().deriveFont(9.0f));
			  g.drawString(Blocks[i].getLine().toString(), 9*(Blocks[i].getLine())/2 + x , y+10);	// Type of block text
			  g.setFont(regFont);
				
				repaint();
		  		revalidate();  
		   */  
	  }	//end for

	  maxWitdh = width;
	  maxHeight = points[i-1].y + 200;

   }
}

